package com.kunya.product.service.impl;

import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.FriendLinkMapper;
import com.kunya.product.po.FriendLink;
import com.kunya.product.service.IFriendLinkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author zhangjiaqing
 * @Description:友情链接接口的实现
 * @date 2018/7/13 16:35
 */
@Service("iFriendLinkService")
public class FriendLinkServiceImpl implements IFriendLinkService{

    @Autowired
    private FriendLinkMapper friendLinkMapper;

    //删除
    @Override
    public ServerResponse removeFriendLink(Long friendlinkId, String tablePrefix) {
        //先判断参数列表中指定参数是否为空，为空返回错误提示信息
        if(friendlinkId==null || StringUtils.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        //执行删除的操作，返回int值
       int deleteResult =friendLinkMapper.deleteByPrimaryKey(friendlinkId,tablePrefix);
        /*
         * 判断：当int等于1时证明一条数据删除成功，当小于等于1时就说没有正确的删除成功，
         * 返回删除失败的错误提示信息
         * 如果以上没有走到，则删除成功，返回删除成功的信息，并且成功时不用返回实体对象给用户，只返成功提示即可
         */
        //优化后的代码
        return deleteResult==1?
                ServerResponse.createBySuccess("删除链接成功"):ServerResponse.createByErrorMessage("删除链接失败！");
    }

    //新增
    @Override
    public ServerResponse addFriendLink(FriendLink friendLink, String tablePrefix) {
        if(friendLink==null || StringUtils.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        friendLink.setCreateTime(new Date());
        friendLink.setUpdateTime(new Date(0));
        int insertResult =friendLinkMapper.insertSelective(friendLink,tablePrefix);
        return insertResult==1?
                ServerResponse.createBySuccess("新建链接成功"):ServerResponse.createByErrorMessage("新建链接失败!");
    }

    //查询
    @Override
    public ServerResponse findFriendLink(Long friendlinkId, String tablePrefix) {
        if (friendlinkId==null || StringUtils.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        FriendLink friendLink=friendLinkMapper.selectByPrimaryKey(friendlinkId,tablePrefix);

        //等于空返回错误信息，不得空在返回成功信息返回查询的数据给用户
        return friendLink ==null?
                ServerResponse.createByErrorMessage("查询链接失败!"):ServerResponse.createBySuccess(friendLink);
    }

    //修改
    @Override
    public ServerResponse<FriendLink> updateFriendLink(FriendLink friendLink, String tablePrefix) {
        if (friendLink==null || StringUtils.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        friendLink.setUpdateTime(new Date());
        int updateResult =friendLinkMapper.updateByPrimaryKeySelective(friendLink,tablePrefix);
        //优化之后的修改
        return updateResult==1?
                ServerResponse.createBySuccess():ServerResponse.createByErrorMessage("修改链接失败！");
    }

    /*
     * 5.后台查询：返回列表
     */
    public  ServerResponse<List<FriendLink>> getFriendLink(Long friendlinkId, String tablePrefix){
        if (friendlinkId==null || StringUtils.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        List<FriendLink> friendLinkList=friendLinkMapper.selectListByFriendLinkId(friendlinkId,tablePrefix);

        return friendLinkList==null?
                ServerResponse.createByErrorMessage("查询链接列表失败！"):ServerResponse.createBySuccess(friendLinkList);
    }
}
